//
//  JUFNTabBar.swift
//  WiBoPM
//
//  Created by 陈俊峰 on 2017/1/4.
//  Copyright © 2017年 陈俊峰. All rights reserved.
//

import UIKit

class JUFNTabBar: UITabBar {
    
    var index = 0
    var callBackClosure:(() -> ())?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.white
        self.addSubview(composeBtn)
        
    }
    @objc private func ClickCompose() {
        callBackClosure?()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        for button in subviews{
            let width = UIScreen.main.bounds.width / 5
            let height = bounds.height
 //要体会在layoutSubviews中添加布局的妙处.另外需要弄清楚tabbar是什么
            //这里能达到效果,但是最好控件要懒加载
//            if button.isKind(of:NSClassFromString("UIButton")!) {
//                button.frame = CGRect.init(x: CGFloat(index) * width, y: 0, width: width, height: height)
//                button.center = CGPoint(x: center.x, y: height * 0.5)
//            }
            composeBtn.bounds.size = CGSize(width: width, height: height)
            composeBtn.center = CGPoint(x: center.x, y: height * 0.5)
            
            if button.isKind(of: NSClassFromString("UITabBarButton")!) {
                button.frame = CGRect.init(x: CGFloat(index) * width, y: 0, width: width, height: height)
                index += (index == 1 ? 2 : 1)
            }
        }

    }
    //不理解,创建空间的的时候,这里的无参无返回值的可以省略,这里有返回值的也可以省略吗?
    lazy var composeBtn:UIButton = {() -> (UIButton)in
        let tempBtn = UIButton()
        tempBtn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: .normal)
        tempBtn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        tempBtn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: .normal)
        tempBtn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: .highlighted)
        tempBtn.addTarget(self, action: #selector(ClickCompose), for: .touchUpInside)
        
        return tempBtn
    }()//注意要加上小括号
    
}
